package lovedna.display {
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * @author lee
	 */
	public class CheckContainer extends Sprite {
		protected var _mousex : Number = 0;
		protected var _mousey : Number = 0;
		protected var _minAlpha : uint = 1;
		protected var _bmd : BitmapData;
		protected var _mat : Matrix;
		protected var _p : Point;

		public function CheckContainer() {
			_bmd = new BitmapData(1, 1, true, 0);
			_mat = new Matrix();
			_p = new Point();
		}

		protected function getUnder(mousex : Number, mousey : Number, one : Boolean = true) : Array {
			_p.x = mousex;
			_p.y = mousey;
			var list : Array = this.getObjectsUnderPoint(_p);
			var len : int = list.length;
			if (len < 1) return null;
			_mat.tx = -mousex;
			_mat.ty = -mousey;
			_bmd.setPixel32(0, 0, 0);
			_bmd.draw(this, _mat);
			var color : uint = _bmd.getPixel32(0, 0) >> 24 & 0xFF;
			if (color < _minAlpha) {
				return null;
			}
			var underlist : Array = [];
			len = this.numChildren;
			while (len--) {
				var child : DisplayObject = getChildAt(len);
				if (!isActive(child)) continue;
				var rect : Rectangle = child.getRect(this);
				if (!rect.contains(mousex, mousey)) continue;
				_bmd.setPixel32(0, 0, 0);
				var p : Point = child.globalToLocal(_p);
				_mat.tx = -p.x;
				_mat.ty = -p.y;
				_bmd.draw(child, _mat);
				color = _bmd.getPixel32(0, 0) >> 24 & 0xFF;
				if (color < _minAlpha) continue;
				underlist.push(child);
				if (one) {
					return underlist;
				}
			}
			list = null;
			child = null;
			return underlist;
		}

		protected function isActive(child : DisplayObject) : Boolean {
			if (child == null) return false;
			if (!child.visible) return false;
			return true;
		}

		public function update() : void {
		}
	}
}
